/**
 * 
 */
package cc.rico.redis.utils;

import java.util.ResourceBundle;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * jedis pool 工具类
 * @author rico 2016年7月4日
 *
 */
public class JedisPoolClientUtil {
	private static final Logger logger = LoggerFactory.getLogger(JedisPoolClientUtil.class);
	// redis host
	private static final String host = "192.168.223.143";
	// redis port
	private static final int port = 6379;

	private static JedisPool jedisPool = null;
	static {
		logger.info("## JedisPoolClientUtil.init...");
		ResourceBundle bundle = ResourceBundle.getBundle("config"); 
		
		GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
		
	    poolConfig.setMaxTotal(Integer.valueOf(bundle.getString("redis.connection.maxTotal")));
	    poolConfig.setMaxIdle(Integer.valueOf(bundle.getString("redis.connection.maxIdle")));
	    poolConfig.setMinIdle(Integer.valueOf(bundle.getString("redis.connection.minIdle")));
	    
		poolConfig.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.connection.testOnBorrow")));
		poolConfig.setTestOnCreate(Boolean.valueOf(bundle.getString("redis.connection.testOnCreate")));
		poolConfig.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.connection.testOnReturn")));
		poolConfig.setTestWhileIdle(Boolean.valueOf(bundle.getString("redis.connection.testWhileIdle")));

		String jedisHost = bundle.getString("redis.connection.host");
		int jedisPort = Integer.valueOf(bundle.getString("redis.connection.port"));
		
		jedisPool = new JedisPool(poolConfig, jedisHost, jedisPort);
	}
	
	private JedisPoolClientUtil() {
		// 构造函数private，禁止通过构造函数生产对象实例
		
	}
	
	public static JedisPool getJedisPoll() {
		return jedisPool;
	}
	
	/**
	 * 从pool获取redis链接
	 * @return
	 */
	public static Jedis getJedis() {
		return jedisPool.getResource();
	}
	
}
